package org.eoti.util.plugin;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileReader;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * The MIT License
 * <p/>
 * Copyright (c) 2009 Malachi de AElfweald (malachid@gmail.com)
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
public class SettingsManager
{
	private Logger logger = Logger.getLogger( getClass().getName() );
	private PluginManager<SettingsPlugin> mgr;
	private File local;
	private String comment;

	public SettingsManager(String comment, String outFilePath)
	{
		this.mgr = new PluginManager<SettingsPlugin>(SettingsPlugin.class);
		this.comment = comment;
		this.local = new File(outFilePath);
		for(SettingsPlugin plugin : mgr)
			plugin.initialize();

		save();
	}

	public SettingsPlugin getPlugin(String key)
	{
		return mgr.getPlugin( key );
	}

	protected File getLocalSettings(){return local;}

	/**
	 * Save configuration
	 * @return true if success, false on failure
	 */
	public boolean save()
	{
		try{
			Properties instanced = new Properties();
			for( SettingsPlugin plugin : mgr)
			{
				Properties pluginProps = plugin.instanced;
				for(String pluginKey : pluginProps.stringPropertyNames())
				{
					instanced.setProperty(
							String.format("%s\\:%s", plugin.getName(), pluginKey),
					    pluginProps.getProperty(pluginKey)
					);
				}
			}
			instanced.store( new FileWriter(local), comment );
			return true;
		} catch( IOException e ){
			logger.severe( String.format("Unable to save configuration: %s", e.getMessage()) );
		}

		return false;
	}

	/**
	 * Load configuration
	 * @return true if success, false on failure
	 */
	protected boolean load()
	{
		try{
			Properties instanced = new Properties();
			instanced.load(new FileReader(local));
			for(String instancedKey : instanced.stringPropertyNames())
			{
				int idx = instancedKey.indexOf(":");
				if(idx == -1)
					throw new IOException(String.format("Invalid key: %s", instancedKey));

				String pluginName = instancedKey.substring(0,idx);
				String pluginKey = instancedKey.substring(idx+1);
				getPlugin(pluginName).instanced.setProperty( pluginKey, instanced.getProperty(instancedKey));
			}
			return true;
		} catch( IOException e ){
			logger.warning( String.format("Unable to load '%s': %s", local.getAbsolutePath(), e.getMessage()) );
		}

		return false;
	}
}
